<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html style="height:100%;">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>文档管理</title>
    <link rel="stylesheet" href="/css/supervise/index.css">
    <link rel="stylesheet" href="/lib/layer/skin/default/layer.css">
    <link rel="stylesheet" href="/lib/laydate/need/laydate.css">
    <link rel="stylesheet" href="/lib/pagination/style/pagination.css">
    <script type="text/javascript" src="/js/jquery/jquery-1.9.1.js"></script>
    <script src="/lib/layer/layer.js?20201106"></script>
    <script src="/js/base/base.js"></script>
    <script src="/lib/laydate/laydate.js"></script>
    <script src="/lib/pagination/js/jquery.pagination.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/ueditor/ueditor.config.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/ueditor/ueditor.all.js" type="text/javascript" charset="utf-8"></script>
    <style>
        .fr{
            float: right;
        }
        .fl{
            float: left;
        }
        .clearfix:after{
            display: block;
            clear:both;
            height:0;
            content: "";
        }
        #table{
            width:100%;
            margin:0 auto;
        }
        .btn{
            width: 105px;
            height: 28px;
            color: #fff;
            cursor: pointer;
            margin-top: 10px;
            border-radius: 3px;
        }

        .retn{
            width: 60px;
            background: rgb(244, 244, 244);
            color: #333333;
            border: 1px solid rgb(220, 220, 220);
        }

        .huan{
            width: 60px;
            background: #eaf1ff;
            color: #333333;
            border: 1px solid #d0def3;
        }

        .empty{
            background: #eaf1ff;
            color: #333333;
            border: 1px solid #d0def3;
        }

        .del{
            width: 60px;
            background: #f0ad4e;
            color: #fff;
            border: 1px solid #eea236;
        }

        .newdocument{
            background: #3079ed;
            border-radius: 3px;
        }
        .deldocument{
            background: #f0ad4e;
            border-radius: 3px;

        }

        .pubic{
            background: #eaf1ff;
            border-radius: 3px;
            color: #333333;
            border: 1px solid #d0def3;
            margin-left: 10px;

        }

        .bin{
            background: #5bc0de;
            border-radius: 3px;
        }

        td{
            font-size: 12px;
            line-height: 35px;

        }

        th{
            font-size: 14px;
        }
        table thead{
            line-height: 35px;
        }

        .svn{
            display: none;
        }



    </style>
</head>
<body style="height:100%;padding-left: 20px">
<div class="svn">
    <div class="bt1">
        <button  class="btn newdocument" >创建文档</button>
        <button class="btn deldocument">删除文档</button>
        <button class="btn pubic">发布指定文档</button>
        <button class="btn bin">文档回收站</button>
    </div>
    <div class="bt2" style="display: none">
        <button class="btn retn" >返回</button>
        <button class="btn huan">还原</button>
        <button class="btn del">删除</button>
        <button class="btn empty">清空回收站</button>

    </div>

</div>
<table  id="table" class="svn">
    <thead>
    <tr>
        <th style="width: 50px"><input id="selectAll"  type="checkbox"></th>
        <th class="th" style="width: 50%;text-align: left">文档标题</th>
        <th class="th">创建时间</th>
        <th class="th" style="width: 8%;">创建人</th>
        <th class="th bt1" style="width: 8%;">所在栏目</th>
        <th class="th bt1" style="width: 8%;">发布状态</th>
        <th class="th bt1" style="width: 8%;">操作</th>
    </tr>
    </thead>
    <tbody >

    </tbody>
</table>

<img src="../img/st.png" alt="" class="tp">
</body>
<script>
    function isInArray(arr,value){//判断数组里是否包含指定元素
        for(var i = 0; i < arr.length; i++){
            if(value === arr[i]){
                return true;
            }
        }
        return false;
    }

    var id=window.parent.extend;//获取父页面id
    var extend1=window.parent.extend1;//得到父元素当前点击extend1

    var columntid=id.substring(id.length-4);//取后4位字符串
    var chnl='chnl';

    var channelId = id.split(';');
    var type=channelId[1];
    channelId = channelId[0]
    if (type=='site'){
        channelId=0;
    }

    $(function () {





        if(channelId != 0){
//            columntid=extendid;
            $('.svn').show()
            $('.tp').hide()
            $.ajax({
                url:'/cmsDocument/datagrid',
                get:'post',
                data:{channelId:channelId,useFlag:false},
                dataType:'json',
                success:function(res){
                    var data=res.data;
                    var str='';
                    for(var i=0;i<data.length;i++){
                        var time=data[i].crTime
                        str+='<tr style="text-align: center" docId='+data[i].chnlDocId+'>' +
                            '<td><input name="multi" value='+data[i].chnlDocId+' type="checkbox"></td>' +
                            '<td class="editor" style="text-align: left;color: #0f92d8;cursor: pointer;">'+function(){if(data[i].docTitle==undefined){return ''}else{return data[i].docTitle}}()+'</td>' +
                            '<td>'+function(){if(time==undefined){return '2000-01-01 00:00:00'}else{return data[i].crTime}}()+'</td>' +
                            '<td>'+function(){if(data[i].crUsername==undefined){return ''}else{return data[i].crUsername}}()+'</td>' +
                            '<td>'+function(){if(data[i].channelName==undefined){return ''}else{return data[i].channelName}}()+'</td>' +
                            '<td>'+function(){
                                if(data[i].status==0){
                                    return '<span style="color: red">未发布</span>';
                                }else{
                                    return '<span style="color: green">已发布</span>';
                                }
                            }()+'</td>' +
                            '<td>'+function () {
                                if(data[i].status==0){
                                    return '<a style="color: green;margin: 0 5px" type="1" id="fabu" href="javascript:void (0)">发布</a>';
                                }else {
                                    return '<a style="color: red;margin: 0 5px" type="0"id="qxfabu" href="javascript:void (0)">停用</a>';
                                }
                            }()+'<a href="#" id="looks">预览</a></td>' +
                            '</tr>'
                    }
                    $('#table tbody').html(str)

                }

            })
        }

        $('#selectAll').bind('click',function(){//全选与不全选
            var $selectAll = $("#selectAll");
            if ($selectAll.prop("checked") == true) {
                // 上面的复选框已被选中
                $(":checkbox[name='multi']").prop("checked", true);
            } else {
                // 上面的复选框没被选中
                $(":checkbox[name='multi']").prop("checked", false);
            }
        })


//删除
        $('.deldocument').click(function(){
            var arr=new Array();
            var isCheck= $("input[name='multi']").is(':checked');//判断是否选中复选框
            if(isCheck==true){
                $.each($("input[name='multi']"),function(){//循环选中的复选框
                    if($(this).is(':checked')){
                        var documentid=$(this).val();//获取被勾选input的value
                        arr.push(documentid)
                    }
                });


                layer.confirm('确认要将所选文档放置回收站吗？', {
                    btn: ['确定','取消'], //按钮
                    title:"确定删除？"
                },function(){
                    $.ajax({
                        url:'/cmsDocument/moveToTrash',
                        type:'get',
                        data:{docids:arr},
                        dataType:'json',
                        success:function(){
                            layer.msg('删除成功', { icon:6,time:1000});
                        }
                    })
                    location.reload();
                })
            }else{
                alert('请至少选择一个要删除的文档')
            }
        });

//发布指定文档

        $('.pubic').click(function(){
            var arr=new Array();
            var isCheck= $("input[name='multi']").is(':checked');//判断是否选中复选框
            if(isCheck==true){
                $.each($("input[name='multi']"),function(){//循环选中的复选框
                    if($(this).is(':checked')){
                        var documentid=$(this).val();//获取被勾选input的value
                        arr.push(documentid)
                    }

                });

                layer.confirm('确认要发布所选文档吗？', {
                    btn: ['确定','取消'], //按钮
                    title:"确定发布？"
                },function(){
                    $.ajax({
                        url:'/cmsDocumentPub/toPub',
                        type:'get',
                        data:{chnlDocIds:arr.join()},
                        dataType:'json',
                        success:function(){
                            layer.msg('发布成功', { icon:6,time:1000});
                        }
                    })
                    location.reload();
                })
            }else{
                alert('请至少选择一个要发布的文档')
            }
        });

//新建
        $('.newdocument').click(function(){
//           $.popWindow("../site/index?"+channelId)
            window.open("../site/index?chnlId="+channelId,'_blank','width=1900,height=960,top=10px,left=0px')
        });
//编辑
        $('#table').on('click','.editor',function(){
            var docId=$(this).parents('tr').attr('docId');
            window.open("../site/editor?docId="+docId +"&chnlId="+channelId,'_blank','width=1900,height=960,top=10px,left=0px')
        })
//发布
        $('#table').on('click','#fabu',function(){
            var docId=$(this).parents('tr').attr('docId');

            layer.confirm('确认要发布所选文档吗？', {
                btn: ['确定','取消'], //按钮
                title:"确定发布？"
            },function(){
                $.ajax({
                    url:'/cmsDocument/updateCmsDocumentStatus',
                    type:'get',
                    data:{sid:docId,status:1},
                    dataType:'json',
                    success:function(){
                        $.layerMsg({content: '发布成功！', icon: 1}, function () {
                            location.reload();
                        })
                    }
                })
            })
        })

        //取消发布
        $('#table').on('click','#qxfabu',function(){
            var docId=$(this).parents('tr').attr('docId');

            layer.confirm('确认要取消发布所选文档吗？', {
                btn: ['确定','取消'], //按钮
                title:"确定取消？"
            },function(){
                $.ajax({
                    url:'/cmsDocument/updateCmsDocumentStatus',
                    type:'get',
                    data:{sid:docId,status:0},
                    dataType:'json',
                    success:function(){
                        $.layerMsg({content: '取消成功！', icon: 1}, function () {
                            location.reload();
                        })
                    }
                })
            })
        })

//预览
        $('#table').on('click','#looks',function(){
            var docId=$(this).parents('tr').attr('docId');
            window.open("/cmsPub/portal?siteId="+extend1+"&chnlDocId="+docId +"&channelId="+channelId)
        })


//        文档回收站
        $('.bin').click(function () {
            $('.bt1').hide()
            $('.bt2').show()
            $.ajax({
                url:'/cmsDocument/datagridTrash',
                get:'post',
                data:{channelId:channelId,useFlag:false},
                dataType:'json',
                success:function(res){
                    var data=res.data;
                    var str='';
                    for(var i=0;i<data.length;i++){
                        var time=data[i].crTime
                        str+='<tr style="text-align: center" docId='+data[i].chnlDocId+'>' +
                            '<td><input name="multi" value='+data[i].chnlDocId+' type="checkbox"></td>' +
                            '<td class="editor" style="text-align: left;color: #0f92d8;cursor: pointer;">'+function(){if(data[i].docTitle==undefined){return ''}else{return data[i].docTitle}}()+'</td>' +
                            '<td>'+function(){if(time==undefined){return '2000-01-01 00:00:00'}else{return data[i].crTime}}()+'</td>' +
                            '<td>'+function(){if(data[i].crUsername==undefined){return ''}else{return data[i].crUsername}}()+'</td>' +
//                            '<td>'+function(){if(data[i].channelName==undefined){return ''}else{return data[i].channelName}}()+'</td>' +
//                            '<td><a href="#" >预览</a></td>' +
//                            '<td><a href="#">发布</a> <a href="#">预览</a></td>' +
                            '</tr>'
                    }
                    $('#table tbody').html(str)

                }

            })
        })

        // 文档回收站（返回）
        $('.retn').click(function () {
            $('.bt2').hide()
            $('.bt1').show()
            $.ajax({
                url:'/cmsDocument/datagrid',
                get:'post',
                data:{channelId:channelId,useFlag:false},
                dataType:'json',
                success:function(res){
                    var data=res.data;
                    var str='';
                    for(var i=0;i<data.length;i++){
                        var time=data[i].crTime
                        str+='<tr style="text-align: center" docId='+data[i].chnlDocId+'>' +
                            '<td><input name="multi" value='+data[i].chnlDocId+' type="checkbox"></td>' +
                            '<td class="editor" style="text-align: left;color: #0f92d8;cursor: pointer;">'+function(){if(data[i].docTitle==undefined){return ''}else{return data[i].docTitle}}()+'</td>' +
                            '<td>'+function(){if(time==undefined){return '2000-01-01 00:00:00'}else{return data[i].crTime}}()+'</td>' +
                            '<td>'+function(){if(data[i].crUsername==undefined){return ''}else{return data[i].crUsername}}()+'</td>' +
                            '<td>'+function(){if(data[i].channelName==undefined){return ''}else{return data[i].channelName}}()+'</td>' +
                            '<td>'+function(){
                                if(data[i].status==0){
                                    return '<span style="color: red">未发布</span>';
                                }else{
                                    return '<span style="color: green">已发布</span>';
                                }
                            }()+'</td>' +
                            '<td>'+function () {
                                if(data[i].status==0){
                                    return '<a style="color: green;margin: 0 5px" type="1" id="fabu" href="javascript:void (0)">发布</a>';
                                }else {
                                    return '<a style="color: red;margin: 0 5px" type="0"id="qxfabu" href="javascript:void (0)">停用</a>';
                                }
                            }()+'<a href="#" id="looks">预览</a></td>' +
                            '</tr>'
                    }
                    $('#table tbody').html(str)

                }

            })
        })
    })

    // 文档回收站（还原）
    $('.huan').click(function(){
        var arr=[];
        var isCheck= $("input[type='checkbox']").is(':checked');//判断是否选中复选框
        if(isCheck==true){
            $.each($('input:checkbox:checked'),function(){//循环选中的复选框
                var documentid=$(this).val();//获取被勾选input的value
                arr.push(documentid)
            });
//                arr.splice(0,1)
//
//                if(isInArray(arr,'on')==true){//判断数组里是否有on
//                   arr.splice(0,1)
//                     arr
//                }else{
//                     arr
//                }
            if(arr[0] == 'on'){
                arr.shift()
            }

            layer.confirm('确定要还原文档吗？', {
                btn: ['确定','取消'], //按钮
                title:"确定还原？"
            },function(){
                $.ajax({
                    url:'/cmsDocument/recycle',
                    type:'get',
                    data:{docids:arr},
                    dataType:'json',
                    success:function(){
                        $.ajax({
                            url:'/cmsDocument/datagridTrash',
                            get:'post',
                            data:{channelId:channelId,useFlag:false},
                            dataType:'json',
                            success:function(res){
                                var data=res.data;
                                var str='';
                                for(var i=0;i<data.length;i++){
                                    var time=data[i].crTime
                                    str+='<tr style="text-align: center" docId='+data[i].chnlDocId+'>' +
                                        '<td><input name="multi" value='+data[i].chnlDocId+' type="checkbox"></td>' +
                                        '<td class="editor" style="text-align: left;color: #0f92d8;cursor: pointer;">'+function(){if(data[i].docTitle==undefined){return ''}else{return data[i].docTitle}}()+'</td>' +
                                        '<td>'+function(){if(time==undefined){return '2000-01-01 00:00:00'}else{return data[i].crTime}}()+'</td>' +
                                        '<td>'+function(){if(data[i].crUsername==undefined){return ''}else{return data[i].crUsername}}()+'</td>' +
//                            '<td>'+function(){if(data[i].channelName==undefined){return ''}else{return data[i].channelName}}()+'</td>' +
//                            '<td><a href="#" >预览</a></td>' +
//                            '<td><a href="#">发布</a> <a href="#">预览</a></td>' +
                                        '</tr>'
                                }
                                $('#table tbody').html(str)

                            }

                        })
                    }
                })
                layer.msg('还原成功', { icon:6,time:1000});

            })
        }else{
            alert('请至少选择一个要还原的文档')
        }
    });


    // 文档回收站（删除）
    $('.del').click(function(){
        var arr=new Array([]);
        var isCheck= $("input[type='checkbox']").is(':checked');//判断是否选中复选框
        if(isCheck==true){
            $.each($('input:checkbox:checked'),function(){//循环选中的复选框
                var documentid=$(this).val();//获取被勾选input的value
                arr.push(documentid)
            });
//                arr.splice(0,1)
//
//                if(isInArray(arr,'on')==true){//判断数组里是否有on
//                   arr.splice(0,1)
//                     arr
//                }else{
//                     arr
//                }

            layer.confirm('确定要删除文档吗？', {
                btn: ['确定','取消'], //按钮
                title:"确定删除？"
            },function(){
                $.ajax({
                    url:'/cmsDocument/deleteDocumentByChnlDoc',
                    type:'get',
                    data:{docids:arr},
                    dataType:'json',
                    success:function(){
                        $.ajax({
                            url:'/cmsDocument/datagridTrash',
                            get:'post',
                            data:{channelId:channelId,useFlag:false},
                            dataType:'json',
                            success:function(res){
                                var data=res.data;
                                var str='';
                                for(var i=0;i<data.length;i++){
                                    var time=data[i].crTime
                                    str+='<tr style="text-align: center" docId='+data[i].chnlDocId+'>' +
                                        '<td><input name="multi" value='+data[i].chnlDocId+' type="checkbox"></td>' +
                                        '<td class="editor" style="text-align: left;color: #0f92d8;cursor: pointer;">'+function(){if(data[i].docTitle==undefined){return ''}else{return data[i].docTitle}}()+'</td>' +
                                        '<td>'+function(){if(time==undefined){return '2000-01-01 00:00:00'}else{return data[i].crTime}}()+'</td>' +
                                        '<td>'+function(){if(data[i].crUsername==undefined){return ''}else{return data[i].crUsername}}()+'</td>' +
                                        '<td>'+function(){if(data[i].channelName==undefined){return ''}else{return data[i].channelName}}()+'</td>' +
//                            '<td><a href="#">发布</a> <a href="#">预览</a> <a href="#" id="editor">编辑</a></td>' +
                                        '<td><a href="#">发布</a> <a href="#">预览</a></td>' +
                                        '</tr>'
                                }
                                $('#table tbody').html(str)

                            }

                        })
                    }
                })
                layer.msg('删除成功', { icon:6,time:1000});

            })
        }else{
            alert('请至少选择一个要删除的文档')
        }
    });

    // 文档回收站（清空）
    $('.empty').click(function(){
        var arr=new Array([]);
        var isCheck= $("input[type='checkbox']").is(':checked');//判断是否选中复选框
//            if(isCheck==true){
//                $.each($('input:checkbox:checked'),function(){//循环选中的复选框
//                    var documentid=$(this).val();//获取被勾选input的value
//                    arr.push(documentid)
//                });
//                arr.splice(0,1)
//
//                if(isInArray(arr,'on')==true){//判断数组里是否有on
//                   arr.splice(0,1)
//                     arr
//                }else{
//                     arr
//                }

        layer.confirm('确定要清空该文档回收站吗？', {
            btn: ['确定','取消'], //按钮
            title:"确定清空？"
        },function(){
            $.ajax({
                url:'/cmsDocument/clearTrash',
                type:'get',
                data:{channelId:channelId},
                dataType:'json',
                success:function(){
                    $.ajax({
                        url:'/cmsDocument/datagridTrash',
                        get:'post',
                        data:{channelId:channelId,useFlag:false},
                        dataType:'json',
                        success:function(res){
                            var data=res.data;
                            var str='';
                            for(var i=0;i<data.length;i++){
                                var time=data[i].crTime
                                str+='<tr style="text-align: center" docId='+data[i].chnlDocId+'>' +
                                    '<td><input name="multi" value='+data[i].chnlDocId+' type="checkbox"></td>' +
                                    '<td class="editor" style="text-align: left;color: #0f92d8;cursor: pointer;">'+function(){if(data[i].docTitle==undefined){return ''}else{return data[i].docTitle}}()+'</td>' +
                                    '<td>'+function(){if(time==undefined){return '2000-01-01 00:00:00'}else{return data[i].crTime}}()+'</td>' +
                                    '<td>'+function(){if(data[i].crUsername==undefined){return ''}else{return data[i].crUsername}}()+'</td>' +
                                    '<td>'+function(){if(data[i].channelName==undefined){return ''}else{return data[i].channelName}}()+'</td>' +
//                            '<td><a href="#">发布</a> <a href="#">预览</a> <a href="#" id="editor">编辑</a></td>' +
                                    '<td><a href="#">发布</a> <a href="#">预览</a></td>' +
                                    '</tr>'
                            }
                            $('#table tbody').html(str)

                        }

                    })
                }
            })
            layer.msg('清空成功', { icon:6,time:1000});

        })
//            }else{
//                alert('请至少选择一个要删除的文档')
//            }
    })




</script>
</html>